package ArrayList.demo02;

import java.util.ArrayList;

public class CheckObject {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<User>();
        //快捷键查找对象构造参数Ctrl+P
        User u1 = new User("zhangsan","suyue001","1234546");
        User u2 = new User("lisi","suyue002","1234546");
        User u3 = new User("wangwu","suyue003","1234546");
        list.add(u1);
        list.add(u2);
        list.add(u3);
        //调用方法查看ID是否存在
        boolean flag = contains(list,"suyue001");
        System.out.println(flag);
    }
    public static boolean contains(ArrayList<User> list, String id) {
/*        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if (u.getId().equals(id)) {
                return true;
            }
        }
        return false;*/
        return getIndex(list, id) >= 0;//通过与0进行比较判断true or false
    }
    public static int getIndex(ArrayList<User> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if (u.getId().equals(id)) {
                return 1;
            }
        }
        return -1;
    }
}
